package com.laiyima.zhongjiang.linghuilv.demo.Serviceprovider;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import com.laiyima.zhongjiang.linghuilv.demo.R;
import com.laiyima.zhongjiang.linghuilv.demo.home.BalanceActivity;
import com.laiyima.zhongjiang.linghuilv.demo.home.DailiMercgantListActivity;
import com.laiyima.zhongjiang.linghuilv.demo.home.ExtensionActivity;
import com.laiyima.zhongjiang.linghuilv.demo.home.IncomeDetailsActivity;
import com.laiyima.zhongjiang.linghuilv.demo.home.MyWalletActivity;
import com.laiyima.zhongjiang.linghuilv.demo.home.NameActivity;
import com.lxj.xpopup.XPopup;
import com.lxj.xpopup.core.CenterPopupView;

import org.json.JSONObject;
import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

public class ServiceproviderActivity extends AppCompatActivity implements View.OnClickListener {
    private String TAG="ServiceproviderActivity";
    private int serCOde,qrCode;


    private LinearLayout to_shanghutuiguang,to_agent_p,to_agent_list,to_my_statics,to_Mercgant,terminal_management,to_my_points,sp_to_balance,sp_to_bill,sp_tomywallet;
    private static final int TIME_EXIT=3000;
    private static int isExit=0;
    //实现按两次后退才退出
    private CountDownTimer mCountDownTimer = new CountDownTimer(TIME_EXIT,1000) {
        @Override
        public void onTick(long l) {
            isExit++;

        }

        @Override
        public void onFinish() {
            isExit=0;
        }
    };

    public boolean onKeyDown(int keyCode, KeyEvent event){
        if(keyCode==KeyEvent.KEYCODE_BACK){
            exit();
            return true;
        }
        return false;
    }
    private void exit(){
        if(isExit<1){
            Toast.makeText(getApplicationContext(),"再点击一次返回退出程序",Toast.LENGTH_SHORT).show();
            mCountDownTimer.start();
        }else{
            isExit=0;
            Toast.makeText(getApplicationContext(),"退出程序",Toast.LENGTH_SHORT).show();
            mCountDownTimer.cancel();
            Intent startMain = new Intent(Intent.ACTION_MAIN);
            startMain.addCategory(Intent.CATEGORY_HOME);
            startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(startMain);
            System.exit(0);
        }
    }



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_serviceprovider);
        to_shanghutuiguang=findViewById(R.id.shanghutuiguang);
        to_agent_p=findViewById(R.id.to_agent_promotion);
        to_agent_list=findViewById(R.id.to_agent_list);
        to_my_statics=findViewById(R.id.to_my_statics);
        terminal_management=findViewById(R.id.terminal_management);
        terminal_management.setOnClickListener(this);
        to_Mercgant=findViewById(R.id.to_Mercgant);
        to_my_points=findViewById(R.id.to_my_points);
        sp_to_balance=findViewById(R.id.sp_to_balance);
        sp_to_balance.setOnClickListener(this);
        sp_to_bill=findViewById(R.id.sp_to_bill);
        sp_tomywallet=findViewById(R.id.sp_tomywallet);
        sp_tomywallet.setOnClickListener(this);
        sp_to_bill.setOnClickListener(this);
        to_agent_p.setOnClickListener(this);
        to_shanghutuiguang.setOnClickListener(this);
        to_agent_list.setOnClickListener(this);
        to_my_statics.setOnClickListener(this);
        to_Mercgant.setOnClickListener(this);
        to_my_points.setOnClickListener(this);
     }

    @Override
    public void onClick(View v) {
        int id=v.getId();
        switch (id){
            case R.id.shanghutuiguang:
                Intent MPintent=new Intent(ServiceproviderActivity.this,MerchantpromotionActivity.class);
                startActivity(MPintent);
                break;
            case R.id.to_agent_promotion:
                Intent ECintent=new Intent(ServiceproviderActivity.this, ExtensionActivity.class);
                startActivity(ECintent);
                break;
            case R.id.to_agent_list:
                Intent ALintent=new Intent(ServiceproviderActivity.this, AgentListActivity.class);
                startActivity(ALintent);
                break;
            case R.id.to_my_statics:
                Intent MyStatisticsintent=new Intent(ServiceproviderActivity.this, MyStatisticsActivity.class);
                startActivity(MyStatisticsintent);
                break;
            case R.id.to_Mercgant:
                Intent Mcintent=new Intent(ServiceproviderActivity.this, DailiMercgantListActivity.class);
                startActivity(Mcintent);
                break;
            case R.id.terminal_management:
                Intent Terintent=new Intent(ServiceproviderActivity.this,TerminalManagementActivity.class);
                startActivity(Terintent);
                break;
            case R.id.to_my_points:
                Intent MyPintent=new Intent(ServiceproviderActivity.this,MyPointsActivity.class);
                startActivity(MyPintent);
                break;
            case R.id.sp_to_balance:
           //     queryreal();
                Intent ABPintent=new Intent(ServiceproviderActivity.this, BalanceActivity.class);
                startActivity(ABPintent);
                break;

            case R.id.sp_to_bill:
                Intent BDintent=new Intent(ServiceproviderActivity.this, IncomeDetailsActivity.class);
                startActivity(BDintent);
                break;

            case R.id.sp_tomywallet:
                Intent WWintent=new Intent(ServiceproviderActivity.this, MyWalletActivity.class);
                startActivity(WWintent);
                break;
        }
    }


    private void dialog_show() {
        ServiceproviderActivity.CustomPopup customPopup = new ServiceproviderActivity.CustomPopup(this);
        new XPopup.Builder(this)
                .asCustom(customPopup)
                .show();
    }
    public  class CustomPopup extends CenterPopupView {
        public CustomPopup(@NonNull Context context) {
            super(context);
        }
        @Override
        protected int getImplLayoutId() {
            return R.layout.toname_popup;
        }
        @Override
        protected void onCreate() {
            super.onCreate();

            findViewById(R.id.toName_close).setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    dismiss();
                }
            });
            findViewById(R.id.toName_dialog_button).setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent IDintent=new Intent(ServiceproviderActivity.this, NameActivity.class);
                    startActivity(IDintent);
                }
            });
        }
        protected void onShow() {
            super.onShow();
        }
    }
    public void queryreal(){
        SharedPreferences sharedPreferences =getSharedPreferences("userinfo.txt", Context.MODE_PRIVATE);
        String userID = sharedPreferences.getString("userID", "");
        RequestParams params = new RequestParams("http://zljl.laiima.com/index.php?s=/lfl_api/Commonlfl/query_real");
        //需要带入的参数
        params.addBodyParameter("uid", userID);
        // params.addBodyParameter("captcha", captcha);
        x.http().post(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {

                Log.e(TAG, "回调成功");
                try {
                    JSONObject jsonObject = new JSONObject(result);
                    qrCode = jsonObject.optInt("code", 0);

                } catch (Exception e) {
                    e.printStackTrace();
                }
                if (qrCode == 200) {
                    Intent Balanceintent=new Intent(ServiceproviderActivity.this,BalanceActivity.class);
                    startActivity(Balanceintent);
                }
                if (qrCode == 404) {
                    dialog_show();
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {

            }
            @Override
            public void onCancelled(CancelledException cex) {

            }
            @Override
            public void onFinished() {

            }
        });

    }
}
